package com.qidian.by.cart.controller;

import com.qidian.by.cart.service.CartService;
import com.qidian.by.cart.vo.CartItemVO;
import com.qidian.by.util.pojo.EsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Author: 小可怎么都睡不够
 * Date: 2024-05-17 11:11
 **/
@RestController
@RequestMapping("/item")
public class CartItemController {
    @Autowired
    private CartService cartService;
    @RequestMapping("/list")
    public EsResult list(HttpServletRequest request){
        return cartService.list(new Long(request.getAttribute("userId").toString()));
    }
    @RequestMapping("/delete")
    public EsResult delete(Long itemId,HttpServletRequest request){
        return cartService.deleteItemFromCart(new Long(request.getAttribute("userId").toString()),itemId);
    }
    @RequestMapping("/update")
    public EsResult update(Long itemId, Integer count,HttpServletRequest request){
        // 从request中获取用户id
        Long userId = new Long(request.getAttribute("userId").toString());
        return cartService.updateItemCount(userId,itemId,count);
    }
    @RequestMapping("/add")
    public EsResult add(@RequestBody CartItemVO vo, HttpServletRequest request){
        // 从request的作用域中获取用户id
        // 我们在登录拦截器中将用户id保存到request作用域中
        Long userId = new Long(request.getAttribute("userId").toString());
        return cartService.addItemToCart(vo,userId);
    }
}
